<template id="edit-dimension-dialog">
    <el-dialog title="权重编辑"
               :visible.sync="editFormVisible"
               width="30%"
               :lock-scroll="false"
               :append-to-body="true"
               :before-close="beforeClose()"
               :show-close="false">
        <el-form :model="editForm" :rules="rules" label-width="80px" ref="editForm">
            <el-form-item label="名称" prop="Name">
                <el-input v-model="editForm.name"/>
            </el-form-item>
            <el-form-item label="权重" prop="weight">
                <el-input v-model="editForm.weight"/>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="cancel">取消</el-button>
            <el-button type="primary" @click="submit">确定</el-button>
        </div>
    </el-dialog>
</template>

<script>
    import {mapActions, mapGetters} from 'vuex';
    import util from "../../common/util";

    export default {
        name: "DimensionEditDialog",
        computed: {
            ...mapGetters('questionnaire', [
                'currentQuestionnaire'
            ])
        },
        data() {
            let checkWeight = (rule, value, callback) => {
                if (value > 1) {
                    callback(new Error("权重不能大于1"));
                }
            };
            return {
                editFormVisible: false,
                editForm: {
                    sort: 1,
                    name: '',
                    weight: 0.0,
                },
                tempName: '',
                rules: {
                    name: [
                        {required: true, message: '名称不能为空', trigger: 'blur'},
                    ],
                    weight: [
                        {validator: checkWeight, trigger: 'blur'},
                        {required: true, message: '权重不能为空', trigger: 'blur'},
                        {type: 'number', required: true, message: '请输入数字', trigger: 'change'}
                    ]
                },
                questionnaire: {}
            }
        },
        methods: {
            ...mapActions('questionnaire', [
                'setCurrentQuestionnaire'
            ]),
            open(dimension) {
                this.editFormVisible = true;
                this.editForm = util.copyObject(dimension);
                this.tempName = dimension.name;
            },
            submit() {
                // if(typeof(this.editForm.weight) !== "number") {
                //     alert("权重只能为小于等于1的数字");
                //     return;
                // }
                this.findBySort(this.editForm.sort, this.questionnaire.dimensions);
                this.setCurrentQuestionnaire(this.questionnaire);
                this.beforeClose();
                this.editFormVisible = false;
            },
            findBySort(sort, dimensions) {
                for (let index in dimensions) {
                    if (dimensions[index].sort === sort && dimensions[index].name === this.tempName) {
                        dimensions[index].name = this.editForm.name;
                        dimensions[index].weight = parseFloat(this.editForm.weight);
                        /**
                         * 修改维度绑定的题目的维度信息。
                         * 维度和题目双向绑定
                         */
                        dimensions[index].questions.forEach(question => {
                            this.questionnaire.questions.forEach(que => {
                                if (que.sort === question.sort) {
                                    que.dimension = this.editForm.name;
                                }
                            })
                        });
                    } else if (dimensions[index].children.length !== 0) {
                        this.findBySort(sort, dimensions[index].children);
                    }
                }
            },
            beforeClose() {
                this.$emit('modalAppendToBody', false);
            },
            cancel() {
                this.editFormVisible = false;
                this.beforeClose();
            }
        },
        watch: {
            currentQuestionnaire() {
                this.questionnaire = this.currentQuestionnaire();
            }
        },
        mounted() {
            this.questionnaire = this.currentQuestionnaire;
        }
    }
</script>

<style lang="scss" scoped>
</style>
